<?php

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\order;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;
use Imi\Config;

/**
 * 售后订单
 * @Auth(name="token",nologin={"aftertype_list"})
 * @Controller("/order.aftersale/")
 */
class Aftersale extends OrderBean
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];



    /**
     * 售后类型列表
     *
     * @param string $type_status 类型:refund=退款,return=退货,change=换货,other=其他
     * @return void
     * @Action
     */
    public function aftertype_list($type_status = null)
    {
        $$where = [];
        if ($type_status)
            $where['type_status'] = $type_status;
        return $this->success($type_status, Db::name('aftertype')->whereEx($where)->orderRaw('weigh desc')->select()->getArray());
    }


    /**
     * 取消售后订单
     *
     * @param int $id 售后订单id
     * @return void
     * @Action
     */
    public function item_cancel_aftersale($id)
    {
        //查询订单取消售后

        $where['id'] = $id;
        $where['user_id'] =  $this->auth->user()->id;

        $order_aftersale = Db::name('order_aftersale')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->find();
        if (!$order_aftersale) {
            return $this->error('没有该订单');
            // $order_aftersale = $this->ItemChangeAftersaleOrderDetail($order_aftersale);
        }
        //判断处理取消

        $res = Db::name('order_aftersale')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->update(['deletetime' => time()]);



        // $order = Db::name('order')->where('id', '=', $order_aftersale['order_id'])->find();
        //直接加余额 积分 贝壳退回
        $订单操作['order_id'] = $order_aftersale['order_id'];
        $订单操作['order_item_id'] = $order_aftersale['id'];
        $订单操作['oper_type'] = 'user'; //user','store','admin','system'
        $订单操作['oper_id'] = $this->auth->user()->id;


        $订单操作['aftersale_status'] = '0'; //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成
        $订单操作['refund_status'] = '0'; //退款状态:-1=拒绝退款,0=未退款,1=同意
        $订单操作['remark'] = '用户取消售后';
        $订单操作['createtime'] = time();
        $订单操作['updatetime'] = time();
        $this->UpdateOrderAction($订单操作);


        $update_order_item['aftersale_status'] = '0'; //售后状态:-1=拒绝,0=未申请,1=申请售后,2=售后完成
        $update_order_item['refund_status'] = '0'; //退款状态:-1=拒绝退款,0=无,1=申请中,2=同意
        Db::name('order_item')->where('id', '=', $order_aftersale['order_item_id'])->update($update_order_item);

        return $this->success('order', $order);
    }




    /**
     * 删除售后订单
     *
     * @param int $id 售后订单id
     * @return void
     * @Action
     */
    public function del_aftersale($id)
    {
        //查询订单取消售后

        $where['id'] = $id;
        $where['user_id'] =  $this->auth->user()->id;

        $order_aftersale = Db::name('order_aftersale')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->find();
        if (!$order_aftersale) {
            return $this->error('没有该订单');
            // $order_aftersale = $this->ItemChangeAftersaleOrderDetail($order_aftersale);
        }
        //判断处理取消


        $res = Db::name('order_aftersale')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->update(['deletetime' => time()]);



        // $order = Db::name('order')->where('id', '=', $order_aftersale['order_id'])->find();
        //直接加余额 积分 贝壳退回
        $订单操作['order_id'] = $order_aftersale['order_id'];
        $订单操作['order_item_id'] = $order_aftersale['id'];
        $订单操作['oper_type'] = 'user'; //user','store','admin','system'
        $订单操作['oper_id'] = $this->auth->user()->id;

        $订单操作['remark'] = '用户删除售后订单' . $id;
        $订单操作['createtime'] = time();
        $订单操作['updatetime'] = time();
        $this->UpdateOrderAction($订单操作);

        return $this->success('删除成功');
    }


    /**
     * 申请平台介入
     *
     * @param int $id
     * @param string $intervene_ps 介入理由
     * @return void
     * @Action
     */
    public function apply_sys_intervene($id, $intervene_ps = null)
    {
        // sys_status
        $where['id'] = $id;
        $where['user_id'] =  $this->auth->user()->id;

        $order_aftersale = Db::name('order_aftersale')
            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->find();
        if (!$order_aftersale) {
            return $this->error('没有该订单');
        }

        if ($order_aftersale['aftersale_status'] == '-1' || $order_aftersale['refund_status'] == '-1') {
            $res = Db::name('order_aftersale')
                ->whereEx($where)
                ->update(['sys_status' => '1', 'intervene_ps' => $intervene_ps]);




            // $order = Db::name('order')->where('id', '=', $order_aftersale['order_id'])->find();
            //直接加余额 积分 贝壳退回
            $订单操作['order_id'] = $order_aftersale['order_id'];
            $订单操作['order_item_id'] = $order_aftersale['id'];
            $订单操作['oper_type'] = 'user'; //user','store','admin','system'
            $订单操作['oper_id'] = $this->auth->user()->id;
            // $订单操作['order_status'] = $order['status']; //订单状态
            // $订单操作['dispatch_status'] = '1'; //发货状态:0=未发货,1=已发货,2=已收货
            // $订单操作['comment_status'] = $order_aftersale['comment_status']; //评价状态:0=未评价,1=已评价
            $订单操作['aftersale_status'] = $order_aftersale['aftersale_status']; //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成
            $订单操作['refund_status'] = $order_aftersale['refund_status']; //退款状态:-1=拒绝退款,0=未退款,1=同意
            $订单操作['remark'] = '用户申请平台介入';
            $订单操作['createtime'] = time();
            $订单操作['updatetime'] = time();
            $this->UpdateOrderAction($订单操作);
        }

        return $this->success('等待平台介入', $order_aftersale);
    }



    /**
     * 获取售后订单列表
     *
     * @param integer|null $page 1
     * @param integer|null $limit 10
     * @param string $refund_status 退款状态:-1=拒绝退款,0=未退款,1=同意
     * @param string $dispatch_status 发货状态:0=未发货,1=已发货,2=已收货
     * @param string $aftersale_status 售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成,3=全部
     * @param string $order 订单号
     * @return void
     * @Action
     */
    public function get_order_aftersale_list(?int $page = 1, ?int $limit = 10, $refund_status = null, $dispatch_status = null, $aftersale_status = null, $order = null)
    {

        $page = $page ?: 1;
        $limit = $limit ?: 10;
        $where['user_id'] = $this->auth->user()->id;



        //退款
        if ($refund_status !== null && $refund_status != '') {
            $where['refund_status'] = $refund_status;
        }

        //售后
        if ($aftersale_status !== null && $aftersale_status != '') {
            $where['aftersale_status'] = $aftersale_status;
        }
        //发货状态
        if ($dispatch_status != null && $dispatch_status != '') {
            $where['dispatch_status'] = $dispatch_status;
        }



        $order_item_ids = Db::name('order_aftersale')->whereEx($where)->whereIsNull('deletetime')->fieldRaw('order_item_id')->column('order_item_id');
        if ($order_item_ids) {
            $list = Db::name('order_item')->whereIn('id', $order_item_ids)->orderRaw('id desc')->page($page, $limit)->select()->getArray();
            if ($list)
                foreach ($list as $key => &$val) {

                    $val = $this->ChangeOrder2($val);
                }

            return $this->success('ok', $list);
        } else {
            return $this->success('ok', []);
        }
        // foreach ($list as $key => &$val) {
        //     $val = $this->ItemChangeAftersaleOrder($val);
        // }
        // return $this->success('ok', $list);
    }


    protected function ChangeOrder2($val)
    {
        $val['store'] = $this->getStoreData2($val['store_id']);
        $val['order'] = Db::name('order')->where('id', '=', $val['order_id'])->fieldRaw('order_sn,status,dispatch_amount')->find();
        $val['aftersale'] = Db::name('order_aftersale')->whereIsNull('deletetime')->where('order_id', '=', $val['order_id'])->where('order_item_id', '=', $val['id'])->find();
        return $val;
    }




    /**
     * 子订单详情👔
     *
     * @param int $id 订单id
     * @return void
     * @Action
     */
    public function order_aftersale_detail(?int $id)
    {
        $where['id'] = $id;
        $where['user_id'] =  $this->auth->user()->id;
        $order = Db::name('order_aftersale')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->select()->get();
        if ($order) {
            $order = $this->ItemChangeAftersaleOrderDetail($order);
        }
        return $this->success('order', $order);
    }




    /**
     * 确认完成（换货订单
     *
     * @param int $id 订单id
     * @return void
     * @Action
     */
    public function order_aftersale_finish(?int $id)
    {
        $where['id'] = $id;
        $where['user_id'] =  $this->auth->user()->id;
        $order_aftersale = Db::name('order_aftersale')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->select()->get();
        if (!$order_aftersale) {
            // $order = $this->ItemChangeAftersaleOrderDetail($order);
            return $this->error('没有该订单');
        }

        //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成
        switch ($order_aftersale['aftersale_status']) {
            case '-1':
                return $this->error('拒绝状态');
                break;
            case '2':
                return $this->error('售后已完成');
                break;
            default:
                # code...
                break;
        }


        //退款状态:-1=拒绝退款,0=未退款,1=同意
        switch ($order_aftersale['refund_status']) {
            case '-1':
                return $this->error('拒绝退款，不能完成');
                break;
                // case '1':
                //     return $this->error('已同意退款不能完成');
                //     break;
            default:
                # code...
                break;
        }

        //只针对退货订单
        $update_order_aftersale['aftersale_status'] = '2'; //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成
        $order_aftersale = Db::name('order_aftersale')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->update($update_order_aftersale);

        return $this->success('order', $order_aftersale);
    }









    /**
     * 子订单申请退款
     *
     * @param int $id 订单id
     * @param float $refund_fee 退款金额
     * @param float $refund_score 退款积分
     * @param float $refund_shell 退款贝壳
     * @param int $aftertype_id 售后类型
     * @param string $ps 留言
     * @param string $images 图集
     * @return void
     * @Action
     */
    public function item_apply_refund(?int $id, ?float $refund_fee = null, ?float $refund_score = null, ?float $refund_shell = null, ?int $aftertype_id = null,  $images = null, ?string $ps = null)
    {

        var_dump('申请退款item_apply_refund' . $id, $refund_fee, $refund_score, $ps);
        $直接到账 = false;
        $where['id'] = $id;
        $where['user_id'] =  $this->auth->user()->id;
        $order_item = Db::name('order_item')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->find();
        if (!$order_item)
            return $this->error('没有该订单');
        $order_data = Db::name('order')->where('id', '=', $order_item['order_id'])->find();



        $data['user_id'] = $this->auth->user()->id;
        $data['order_id'] = $order_data['id'];
        $data['order_item_id'] = $id;
        $data['type'] = 'refund'; //类型:refund=退款,return=退货,other=其他
        $res = Db::name('order_aftersale')->whereEx($data)->whereIsNull('deletetime')->find();
        if ($res) {
            if ($res['aftersale_status'] != '-1')
                return $this->error('该订单已申请过退款');
        }

        if ($refund_fee > ($order_item['pay_fee']  + $order_data['dispatch_amount'])) {
            return $this->error('大于购买商品的价格的钱', $order_item['pay_fee'] + $order_data['dispatch_amount']);
            $data['refund_fee'] = $refund_fee;
        } elseif (!$refund_fee || $refund_fee < 0) {
            // return $this->error('请输入退款金额');
            $data['refund_fee'] = $order_item['pay_fee'];
        } else {
            $data['refund_fee'] = $refund_fee;
        }

        // if ($refund_shell > $order_item['item_shell_fee'] * $order_item['goods_num']) {
        if ($refund_shell > $order_item['shell_fee']) {
            return $this->error('大于购买商品的贝壳', $order_item['item_shell_fee'] * $order_item['goods_num']);
        } else {
            // $data['refund_shell'] = $order_item['item_shell_fee'] * $order_item['goods_num'];
            $data['refund_shell'] = $order_item['shell_fee'];
        }

        if ($refund_score > $order_item['score_fee']) {
            return $this->error('大于购买商品的积分', $order_item['score_fee']);
        } else {
            $data['refund_score'] = $order_item['score_fee'];
            // $data['refund_score'] = $refund_score?$refund_score:0;
        }

        // $data['refund_fee'] = $refund_fee > 0 ? $refund_fee : $order_item['pay_fee'];



        if ($aftertype_id) {
            $data['type_str'] = Db::name('aftertype')->where('id', '=', $aftertype_id)->value('name');
        } else {
            return $this->error('请选择售后类型');
        }


        $data['aftersale_sn'] = rand_order('Refund' . $id, $this->auth->user()->id); //售后单号


        $data['phone'] = $order_data['phone'];
        $data['ps'] = $ps;

        $data['goods_id'] = $order_item['goods_id'];
        $data['goods_sku_price_id'] = $order_item['goods_sku_price_id'];

        $data['goods_sku_text'] = $order_item['goods_sku_text'];
        $data['goods_title'] = $order_item['goods_title'];
        $data['goods_image'] = $order_item['goods_image'];

        $data['goods_original_price'] = $order_item['goods_original_price'];
        $data['discount_fee'] = $order_item['discount_fee'];
        $data['goods_price'] = $order_item['goods_price'];
        $data['goods_num'] = $order_item['goods_num'];
        $data['dispatch_status'] = $order_item['dispatch_status']; //发货状态:0=未发货,1=已发货,2=已收货
        $data['dispatch_fee'] = $order_item['dispatch_fee']; //发货费用


        $data['store_id'] = $order_item['store_id'];

        $data['images'] = $images;
        $data['createtime'] = time();

        ////发货状态:0=未发货,1=已发货,2=已收货         订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
        if ($order_item['dispatch_status'] == '0' && $order_item['status'] == '1') {
            //直接到账
            $直接到账 = true;
            $data['refund_status'] = '1'; //退款状态:-1=拒绝退款,0=未退款,1=同意
            $data['aftersale_status'] = '2'; //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成
        } else {
            //商家审核

            $data['refund_status'] = '0'; //退款状态:-1=拒绝退款,0=未退款,1=同意
            $data['aftersale_status'] = '0'; //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成
        }


        $order_aftersale_id = Db::name('order_aftersale')->insert($data)->getLastInsertId();

        //`aftersale_status` tinyint(2) NOT NULL COMMENT '售后状态:-1=拒绝,0=未申请,1=申请售后,2=售后完成',
        //  `refund_status` tinyint(1) DEFAULT NULL COMMENT '退款状态:-1=拒绝退款,0=无,1=申请中,2=同意',
        Db::name('order_item')->where('id', '=', $id)->update(['aftersale_status' => '1', 'refund_status' => '1', 'updatetime' => time()]);
        if (!$直接到账) {

            // $order = Db::name('order')->where('id', '=', $order_item['order_id'])->find();
            //直接加余额 积分 贝壳退回
            $订单操作['order_id'] = $order_item['order_id'];
            $订单操作['order_item_id'] = $order_item['id'];
            $订单操作['oper_type'] = 'user'; //user','store','admin','system'
            $订单操作['oper_id'] = $this->auth->user()->id;
            // $订单操作['order_status'] = $order['status']; //订单状态
            // $订单操作['dispatch_status'] = '1'; //发货状态:0=未发货,1=已发货,2=已收货
            // $订单操作['comment_status'] = $order_item['comment_status']; //评价状态:0=未评价,1=已评价
            $订单操作['aftersale_status'] = '1'; //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成
            $订单操作['refund_status'] = '1'; //退款状态:-1=拒绝退款,0=无,1=申请中,2=同意',
            $订单操作['remark'] = '用户申请退款';
            $订单操作['createtime'] = time();
            $订单操作['updatetime'] = time();
            $this->UpdateOrderAction($订单操作);

            return $this->success('请等待商家处理');
        } else {
            var_dump('直接到账');
            $订单操作['order_id'] = $order_item['order_id'];
            $订单操作['order_item_id'] = $order_item['id'];
            $订单操作['oper_type'] = 'user'; //user','store','admin','system'
            $订单操作['oper_id'] = $this->auth->user()->id;
            // $订单操作['order_status'] = $order['status']; //订单状态
            // $订单操作['dispatch_status'] = '1'; //发货状态:0=未发货,1=已发货,2=已收货
            // $订单操作['comment_status'] = $order_item['comment_status']; //评价状态:0=未评价,1=已评价
            $订单操作['aftersale_status'] = '2'; //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成
            $订单操作['refund_status'] = '2'; //退款状态:-1=拒绝退款,0=无,1=申请中,2=同意',
            $订单操作['remark'] = '用户申请退款,未发货,直接到账';
            $订单操作['createtime'] = time();
            $订单操作['updatetime'] = time();
            $this->UpdateOrderAction($订单操作);



            $order_aftersale = $data;

            $order = $this->getOrderById($order_aftersale['order_id']);


            // $this->UpdateOrderAction($data);
            Db::name('order_aftersale')
                ->whereIsNull('deletetime')
                ->where('id', '=', $order_aftersale_id)
                ->update(['refund_status' => '1', 'updatetime' => time(), 'aftersale_status' => '2']);

            // if ($order_aftersale['refund_score'] > 0) {
            //     $this->IncUserScore($order_aftersale['user_id'], (float)$order_aftersale['refund_score'], '未发货申请退款', 13, $id);
            // }

            // if ($order_aftersale['refund_shell'] > 0) {
            //     $this->IncUsershell($order_aftersale['user_id'], (float)$order_aftersale['refund_shell'], '未发货申请退款', 13, $id);
            // }

            // //如果是微信和支付宝支付,原路退回
            // if ($order_aftersale['refund_fee'] > 0) {
            //     // $order
            //     $this->IncUserMoney($order_aftersale['user_id'], (float)$order_aftersale['refund_fee'], '未发货申请退款', 13, $id);
            // }





            $update_order_item['refund_status'] = '2'; //退款状态:-1=拒绝退款,0=无,1=申请中,2=同意
            $update_order_item['updatetime'] = time(); //
            $update_order_item['aftersale_status'] = '2'; //售后状态:-1=拒绝,0=未申请,1=申请售后,2=售后完成
            $update_order_item['status'] = '-2'; //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成

            Db::name('order_item')->where('id', '=', $order_aftersale['order_item_id'])->update($update_order_item);
            //如果这个最后一个退款,记得连运费一起退回
            $this->storeOrderRefund($order_aftersale_id);
            return $this->success('退款成功');
        }
    }


    /**
     * 子订单申请售后
     *
     * @param int $id 订单id
     * @param string $ps 备注
     * @param string $type refund=退款,return=退货,change=换货,other=其他
     * @param string $dispatch_status 发货状态:0=未发货,1=未收到,2=已收货
     * @param string $images 图集
     * @return void
     * @Action
     */
    public function item_apply_aftersale(?int $id, $ps = null, $type = null, ?float $refund_fee = null, ?float $refund_score = null, ?float $refund_shell = null, ?int $aftertype_id = null, $images = null, $dispatch_status = '1')
    {

        var_dump('申请售后item_apply_aftersale' . $id, $type, $refund_fee, $refund_score, $ps);

        $where['id'] = $id;
        $where['user_id'] =  $this->auth->user()->id;
        $order_item = Db::name('order_item')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->find();
        if (!$order_item)
            return $this->error('没有该订单');
        $order_data = Db::name('order')->where('id', '=', $order_item['order_id'])->find();



        $data['user_id'] = $this->auth->user()->id;
        $data['order_id'] = $order_data['id'];
        $data['order_item_id'] = $id;
        switch ($type) {
            case 'refund':
                //退款
                $str = '退款';

                break;
            case 'return':
                //退货
                $str = '退货';

                break;
            case 'change':
                //其他
                $str = '换货';

                break;
            default:
                $str = '其他';

                break;
        }
        $data['type'] = $type; //类型:refund=退款,return=退货,other=其他
        $res = Db::name('order_aftersale')->whereEx($data)->whereIsNull('deletetime')->find();
        if ($res) {
            return $this->error('该订单已申请过退款');
        }

        if ($refund_fee > ($order_item['pay_fee']  + $order_data['dispatch_amount'])) {
            return $this->error('大于购买商品的价格的钱', $order_item['pay_fee'] + $order_data['dispatch_amount']);
        } else {
            $data['refund_fee'] = $refund_fee;
        }

        // if ($refund_shell > $order_item['item_shell_fee'] * $order_item['goods_num']) {
        if ($refund_shell > $order_item['shell_fee']) {
            return $this->error('大于购买商品的贝壳', $order_item['shell_fee']);
        } else {
            $data['refund_shell'] = $refund_shell;
        }

        if ($refund_score > $order_item['score_fee']) {
            return $this->error('大于购买商品的积分', $order_item['score_fee']);
        } else {

            $data['refund_score'] = $refund_score;
        }

        // $data['refund_fee'] = $refund_fee > 0 ? $refund_fee : $order_item['pay_fee'];



        if ($aftertype_id) {
            $data['type_str'] = Db::name('aftertype')->where('id', '=', $aftertype_id)->value('name');
        } else {
            return $this->error('请选择售后类型');
        }

        $data['aftersale_sn'] = rand_order('Refund' . $id, $this->auth->user()->id); //售后单号

        $data['images'] = $images;
        $data['phone'] = $order_data['phone'];
        $data['ps'] = $this->Xss($ps);

        $data['goods_id'] = $order_item['goods_id'];
        $data['goods_sku_price_id'] = $order_item['goods_sku_price_id'];

        $data['goods_sku_text'] = $order_item['goods_sku_text'];
        $data['goods_title'] = $order_item['goods_title'];
        $data['goods_image'] = $order_item['goods_image'];

        $data['goods_original_price'] = $order_item['goods_original_price'];
        $data['discount_fee'] = $order_item['discount_fee'];
        $data['goods_price'] = $order_item['goods_price'];
        $data['goods_num'] = $order_item['goods_num'];
        $data['dispatch_status'] = $order_item['dispatch_status']; //发货状态:0=未发货,1=已发货,2=已收货
        $data['dispatch_fee'] = $order_item['dispatch_fee']; //发货费用
        $data['aftersale_status'] = '0'; //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成
        $data['refund_status'] = '0'; //退款状态:-1=拒绝退款,0=未退款,1=同意
        $data['store_id'] = $order_item['store_id'];

        $data['dispatch_status'] = $dispatch_status ?: '0'; //发货状态:0=未发货,1=未收到,2=已收货 和普通的里面有点不一样


        $data['createtime'] = time();

        $res = Db::name('order_aftersale')->insert($data);
        Db::name('order_item')->where('id', '=', $id)->update(['aftersale_status' => '1', 'updatetime' => time()]);
        if ($res) {

            $order = Db::name('order')->where('id', '=', $order_item['order_id'])->find();
            //直接加余额 积分 贝壳退回
            $订单操作['order_id'] = $order_item['order_id'];
            $订单操作['order_item_id'] = $order_item['id'];
            $订单操作['oper_type'] = 'user'; //user','store','admin','system'
            $订单操作['oper_id'] = $this->auth->user()->id;

            $订单操作['aftersale_status'] = '0'; //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成

            $订单操作['remark'] = '用户申请售后' . $str;
            $订单操作['createtime'] = time();
            $订单操作['updatetime'] = time();
            $this->UpdateOrderAction($订单操作);

            return $this->success('请等待商家处理');
        }
    }

    /**
     * 上传退货单号
     * 
     * @param int $id 售后订单id
     * @param string $express_name 快递公司
     * @param string $express_no 快递编号
     * @Action
     */
    public function upload_express_no($id, $express_name = null, $express_no = null)
    {
        $where['id'] = $id;
        $where['user_id'] =  $this->auth->user()->id;
        $order_aftersale = Db::name('order_aftersale')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->select()->get();
        if (!$order_aftersale) {
            return $this->error('没有该订单');
        }

        $update_data['express_name'] = $express_name;
        $update_data['express_no'] = $express_no;

        Db::name('order_aftersale')
            ->whereIsNull('deletetime')
            ->whereEx($where)
            ->update($update_data);


        $快递包裹['user_id']  = $order_aftersale['user_id']; //用户
        $快递包裹['store_id']  = $order_aftersale['store_id']; //店铺
        $快递包裹['order_id']  = $order_aftersale['order_id']; //订单
        $快递包裹['order_item_id']  = $order_aftersale['order_item_id']; //子订单
        $快递包裹['express_name']  = $express_name; //快递公司
        $快递包裹['express_no']  = $express_no; //快递单号
        $快递包裹['createtime']  = time();
        $快递包裹['updatetime']  = time();
        $快递包裹['status']  = '2'; //状态:1=卖家正常发货,2=用户售后,3=卖家换货
        $this->AddOrderExpress($快递包裹);

        return $this->success('order', $order_aftersale);
    }





    /**
     * 售后
     *
     * @param array $val
     * @return void
     */
    protected function ItemChangeAftersaleOrder($val)
    {
        $val['store'] = $this->getStoreData2($val['store_id']);
        $val['order_item'] = Db::name('order_item')->where('id', '=', $val['order_item_id'])->find();
        $val['order'] = Db::name('order')->where('id', '=', $val['order_id'])->find();
        $val['date'] = date('Y-m-d H:i:s', $val['createtime']);
        return $val;
    }



    /**
     * 售后
     *
     * @param array $val
     * @return void
     */
    protected function ItemChangeAftersaleOrderDetail($val)
    {
        $val['store'] = $this->getStoreData2($val['store_id']);
        $val['order_item'] = Db::name('order_item')->where('id', '=', $val['order_item_id'])->find();
        $order = Db::name('order')->where('id', '=', $val['order_id'])->find();
        if ($order) {
            $order['date'] = date('Y-m-d H:i:s', $order['createtime']);
            $val['order'] = $order;
        }
        $val['date'] = date('Y-m-d H:i:s', $val['createtime']);

        if ($val['type'] == 'change' || $val['type'] == 'return') {
            $where['order_id'] = $val['order_id'];
            $where['order_item_id'] = $val['order_item_id'];
            $val['order_express'] = Db::name('order_express')->whereEx($where)->orderRaw('id desc')->select()->getArray();
        }

        return $val;
    }
}
